# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
 
@File:merge_docx1_0.py
@Time:2024/8/28 8:51

@Motto:不积跬步无以至千里，不积小流无以成江海！
"""
import os
from docx import Document


def merge_word_files(folder_path):
    merged_doc = Document()
    file_list = [f for f in os.listdir(folder_path) if f.endswith('.docx')]
    for file_name in file_list:
        file_path = os.path.join(folder_path, file_name)
        try:
            doc = Document(file_path)
            for para in doc.paragraphs:
                merged_doc.add_paragraph(para.text)
                # 如果需要，可以添加更多的元素，如表格、图片等
        except Exception as e:
            print(f"Error processing file {file_path}: {e}")
    return merged_doc


folder_path = '全国统计公报/'
merged_document = merge_word_files(folder_path)
merged_document.save('merged1_0.docx')